package com.earthquake.monitor_system.controller;

import com.earthquake.monitor_system.controller.resultType.Pagination;
import com.earthquake.monitor_system.controller.resultType.UserInfoType;
import com.earthquake.monitor_system.domain.UserDomain;
import com.earthquake.monitor_system.service.impl.UserImpl;
import com.earthquake.monitor_system.utils.Result;
import com.earthquake.monitor_system.utils.status;
import com.earthquake.monitor_system.utils.successCode.ImageCaptchaResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class User  {
    @Autowired
    private UserImpl userImpl;
    private String code;
    @GetMapping("/findAll")
    public Result getUser() {
        List<UserDomain> user = userImpl.getUser();
        return Result.success("获取数据成功",user);
    }
    @PostMapping("/register")
    public Result register(@RequestBody UserDomain userDomain) {
        Boolean register = userImpl.register(userDomain.getUsername(), userDomain.getPassword(), userDomain.getPhone());
        if(!register) {
            Result<Boolean> Result = new Result<>(status.STATUS_WARN,"注册失败!用户已存在!", false);
            return Result;
        }
        Result<Boolean> Result = new Result<>("注册成功", true);
        return Result;
    }
    @PostMapping("/login")
    public Result login(@RequestBody UserDomain userDomain) {
        List<UserInfoType> result = userImpl.login(userDomain.getUsername(), userDomain.getPassword(),userDomain.getCode());
        if(!userDomain.getCode().toLowerCase().equals(this.code.toLowerCase())) {
            return Result.warn("验证码错误!", null);
        }
        if(result.size() == 0) {
            return Result.warn("登录失败!用户名密码错误！", null);
        }
        return Result.success("登陆成功",result);
    }
    @GetMapping("/code")
    public Result successCode() throws IOException {
       try {
           ImageCaptchaResult imageCaptchaResult = userImpl.successCode();
           System.out.println(imageCaptchaResult.getWords());
           this.code = imageCaptchaResult.getWords();
           Map<String,Object> map = new HashMap<>();
           map.put("status", true);
           map.put("base64Url", imageCaptchaResult.getReturnContent());
           return Result.success("获取成功!",map);
       }catch (Exception e) {
           Map<String,Object> map = new HashMap<>();
           map.put("status", false);
           map.put("base64Url", null);
           return Result.error("获取失败!",map);
       }
    };
    @PostMapping("/queryUserInfoList")
    public Result querySeismicInfoList(@RequestBody Pagination pagination) {
        Object seismicInformations = userImpl.queryUserInfoList(pagination);
        return Result.success("获取数据成功",seismicInformations);
    }
    @DeleteMapping("/deleteById")
    public Result deleteById(String id) {
        Boolean aBoolean = userImpl.deleteById(id);
        return Result.success("请求成功",aBoolean);
    }
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody UserDomain userDomain){
        String password = userDomain.getPassword();
        String phone = userDomain.getPhone();
        String username = userDomain.getUsername();
        Boolean aBoolean = userImpl.changePassword(password, username, phone);
        if(!aBoolean) {
            return Result.success("修改密码失败！请检查你填写的手机号码！",false);
        }
        return Result.success("修改密码成功！",true);
    }
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestBody UserDomain userDomain,@RequestHeader("token") String token) {
        Boolean aBoolean = userImpl.uploadAvatar(userDomain.getAvatar(), token);
        if(!aBoolean) {
            return Result.success("上传失败！", false);
        }
        return Result.success("上传成功！", true);
    }
    @PostMapping("/changeUserInfo")
    public Result changeUserInfo(@RequestBody UserDomain UserDomain,@RequestHeader("token") String token){
        Boolean aBoolean = userImpl.changeUserInfo(UserDomain, token);
        if(!aBoolean) {
            return Result.success("修改失败！", false);
        }
        return Result.success("修改成功！", true);
    }
}
